package com.efounder.form.comp;

import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.text.util.Linkify;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.model.JSONFormModel;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.util.AppContext;
import com.efounder.util.StringUtil;

import static com.efounder.form.util.FormCompUtil.getLayoutGravity;

/**
 * Created by Javor on 16/9/7.
 */

public class FormLabel extends TextView implements IComponent,IUIComponent,DMComponent,DMColComponent,DataSetListener {

    private String id;

    private int height;
    private int width;

    private int gap = 0;

    private String fontSize = "16";

    private ViewSize size = new ViewSize(0,0);

    private int percentWidth;
    private int percentHeight;

    private String originalText;

    private String dataSetID;
    private String dataSetColID;

    private String horizontalAlign;
    private String verticalAlign;

    private DataSetComponent dataSetComponent;
    private EFDataSet dataSet;
    private EFRowSet mainRowSet;

    private int bkgColor;

    private String textAlign = "left";

    private String fontWeight;

    private IUIComponent parentComp;

    private boolean visible;

    private boolean autoLink;

    public FormLabel() {
        super(AppContext.getInstance());
        this.setTextColor(Color.BLACK);
    }

    public FormLabel(Context context) {
        super(context);
    }

    public FormLabel(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void dataChanged(EFDataSet dataSet) {
        mainRowSet = dataSet.getCurrentRowSet();
        String value = "";
        if (mainRowSet != null){
            value = mainRowSet.getString(dataSetColID, "");
        }

        this.setText(value);
    }

    public String getOriginalText() {
        return originalText;
    }

    public void setOriginalText(String originalText) {
        this.originalText = originalText;

        this.setText(originalText);
    }


    public String getFontSize() {
        return fontSize;
    }

    public void setFontSize(String fontSize) {
        this.fontSize = fontSize;
    }

    @Override
    public DataSetComponent getDataSetComponent() {
        return dataSetComponent;
    }

    @Override
    public void setDataSetComponent(DataSetComponent dsc) {
        if (dataSetComponent != null)
            dataSetComponent.removeDMComponent(this);
        dataSetComponent = dsc;
        if (dataSetComponent != null)
            dataSetComponent.insertDMComponent(this);
    }

    public int getBkgColor() {
        return bkgColor;
    }

    public void setBkgColor(int bkgColor) {
        this.bkgColor = bkgColor;

        this.setBackgroundColor(StringUtil.formColorToColor(bkgColor));
    }

    @Override
    public void setLayout() {
        FormCompUtil.setLayoutSize(this);
        if (getLayoutParams() instanceof LinearLayout.LayoutParams){
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
            params.gravity = getLayoutGravity(horizontalAlign,verticalAlign);
        }
    }

    @Override
    public void creationComplete() {
        //字体大小
        try {
            setTextSize(Integer.parseInt(fontSize));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //对齐方式
        if ("left".equals(textAlign) || "start".equals(textAlign)){
            setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        }else if ("right".equals(textAlign)){
            setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        }else {
            setGravity(Gravity.CENTER);
        }
        //字体加粗
        if ("bold".equals(fontWeight)){
            TextPaint textPaint = getPaint();
            textPaint.setFakeBoldText(true);
        }

//        this.setGravity(getLayoutGravity(horizontalAlign,verticalAlign));
    }

    public String getHorizontalAlign() {
        return horizontalAlign;
    }

    public void setHorizontalAlign(String horizontalAlign) {
        this.horizontalAlign = horizontalAlign;
    }

    public String getVerticalAlign() {
        return verticalAlign;
    }

    public void setVerticalAlign(String verticalAlign) {
        this.verticalAlign = verticalAlign;
    }

    @Override
    public void setDataSetID(String dataSetID) {
        this.dataSetID = dataSetID;
    }

    @Override
    public String getDataSetColID() {
        return dataSetColID;
    }

    @Override
    public void setDataSetColID(String dataSetColID) {
        this.dataSetColID = dataSetColID;
    }

    @Override
    public String getDataSetNameColID() {
        return null;
    }

    @Override
    public void setDataSetNameColID(String value) {

    }

    @Override
    public String getInternalDataSetID() {
        return null;
    }

    @Override
    public void setInternalDataSetID(String dataSetID) {

    }

    @Override
    public void setViewDataSetID(String viewDataSetID) {

    }

    @Override
    public String getViewDataSetID() {
        return null;
    }

    @Override
    public void setViewDataSetColID(String viewDataSetColID) {

    }

    @Override
    public String getViewDataSetColID() {
        return null;
    }

    @Override
    public void setValueDataSetColID(String valueDataSetColID) {

    }

    @Override
    public String getValueDataSetColID() {
        return null;
    }

    @Override
    public Boolean getIsUserInternalDataSetID() {
        return null;
    }

    @Override
    public void setIsUserInternalDataSetID(Boolean v) {

    }

    @Override
    public String getIdentifier() {
        return null;
    }

    @Override
    public Object getPropertyMap() {
        return null;
    }

    @Override
    public void setPropertyMap(Object value) {

    }

    @Override
    public Object getProperty(String key, Object defVal) {
        return null;
    }

    @Override
    public void setProperty(String key, Object value) {

    }

    @Override
    public int getNumberPrecision() {
        return 0;
    }

    @Override
    public void seNumberPrecision(int value) {

    }

    @Override
    public String getNumberFormat() {
        return null;
    }

    @Override
    public void setNumberFormat(String numberFormat) {

    }

    @Override
    public String getColumnType() {
        return null;
    }

    @Override
    public void setColumnType(String value) {

    }

    @Override
    public String getEditorType() {
        return null;
    }

    @Override
    public void setEditorType(String value) {

    }

    @Override
    public String getDateFormat() {
        return null;
    }

    @Override
    public void setDateFormat(String dateFormat) {

    }

    @Override
    public String getFormulaOne() {
        return null;
    }

    @Override
    public void setFormulaOne(String formulaOne) {

    }

    @Override
    public String getEditMask() {
        return null;
    }

    @Override
    public void setEditMask(String editMask) {

    }

    @Override
    public String getFkeyID() {
        return null;
    }

    @Override
    public void setFkeyID(String fkey) {

    }

    @Override
    public String getRlglID() {
        return null;
    }

    @Override
    public void setRlglID(String rlglID) {

    }

    @Override
    public String getDataSetID() {
        return dataSetID;
    }

    @Override
    public void setDataSet(EFDataSet dataSet) {
        if ( this.dataSet != dataSet ) {
            if ( this.dataSet != null ) this.dataSet.removeDataSetListener(this);
            this.dataSet = dataSet;
            if ( this.dataSet != null ) this.dataSet.addDataSetListener(this);
            this.dataChanged(dataSet);
        }
    }

    @Override
    public EFDataSet getDataSet() {
        return dataSet;
    }

    @Override
    public EFRowSet getMainRowSet() {
        return mainRowSet;
    }

    @Override
    public void dataSetChanged(DataSetEvent event) {
        dataChanged(event.getDataSet());
    }

    @Override
    public String getID() {
        return id;
    }

    @Override
    public void setID(String id) {
        this.id = id;
    }

    @Override
    public int getGap() {
        return gap;
    }

    @Override
    public void setGap(int gap) {
        this.gap = gap;
    }

    @Override
    public void setCompWidth(int width) {
        this.width = width;
    }

    @Override
    public int getCompWidth(){
        return this.width;
    }

    @Override
    public void setCompHeight(int height) {
        this.height = height;
    }

    @Override
    public int getCompHeight() {
        return this.height;
    }

    @Override
    public int getPercentWidth() {
        return percentWidth;
    }

    @Override
    public void setPercentWidth(int percentW) {
        percentWidth = percentW;
    }

    @Override
    public int getPercentHeight() {
        return percentHeight;
    }

    @Override
    public void setPercentHeight(int percentH) {
        percentHeight = percentH;
    }

    @Override
    public ViewSize getViewSize() {
        return size;
    }

    @Override
    public void setViewSize(ViewSize size) {
        this.size = size;
    }

    public String getTextAlign() {
        return textAlign;
    }

    public void setTextAlign(String textAlign) {
        this.textAlign = textAlign;

    }

    public String getFontWeight() {
        return fontWeight;
    }

    public void setFontWeight(String fontWeight) {
        this.fontWeight = fontWeight;
    }

    @Override
    public IUIComponent getParentComp() {
        return parentComp;
    }

    @Override
    public void setParentComp(IUIComponent comp) {
        parentComp = comp;
    }

    public boolean isAutoLink() {
        return autoLink;
    }

    public void setAutoLink(boolean autoLink) {
        this.autoLink = autoLink;
        if(autoLink)
            this.setAutoLinkMask(Linkify.PHONE_NUMBERS);
    }
}
